Repository navigation
fix(player): reset playback speed to 1x at the live head - #4347
Conversation
wahajahmed010
left a comment
There was a problem hiding this comment.
Review
Solid bug fix with good test coverage. The diagnosis is excellent: measuring the difference between YouTube's API speed and direct video.playbackRate manipulation and tying it to the missing live-head reset is the kind of root-cause work that ages well. Notes:
1. Hook placement is correct. Binding playerLiveHeadSpeedReset to waiting in playerOnPlay (and unbinding/rebinding on each play) matches how the existing playerOnEnded listener is managed right above it. Consistent with the surrounding code style.
2. The four guards are all necessary:
video.playbackRate > 1(only relevant when sped up; no work at 1x)video.closest('.html5-video-player')with fallback toImprovedTube.elements.player(covers environments whereclosestdoesn't return the player, e.g., older YouTube layouts)getVideoData().isLive(only applies to live streams)getProgressState().isAtLiveHead === true(only when actually at the live head, not while DVR-watching behind it)
The test suite exercises each of these branches. Excellent coverage.
3. isAtLiveHead !== true (strict). You correctly use strict inequality, so undefined (older API) bails out. If isAtLiveHead is missing because the API isn't available, you do nothing. Safe default.
4. showStatus(1) UX. Resetting the speed at the live head is silent to the user beyond a status toast. Some users may find the sudden drop from 5x to 1x jarring; a future enhancement could be to ramp the speed gradually. Not for this PR, just a thought.
5. waiting is the right event. stalled would also work but fires less reliably across browsers. waiting is the HTML5 spec's official "no data right now" event. Good choice.
6. The test harness is well-built. Using vm.runInNewContext with a minimal ImprovedTube shim and setup({...}) factory is much cleaner than monkey-patching globals. The 5 test cases cover the main branches and an isAtLiveHead !== true boundary. The test.each([1, 0.75]) parameterization for non-sped-up rates is a nice touch.
7. One nit on test coverage. You don't test the closest() fallback path (when closest returns null, the code falls back to ImprovedTube.elements.player). It's exercised by the "does nothing when the player API is unavailable" test indirectly, but a dedicated test where closest returns null and elements.player is undefined would prove the early-return path more directly. Optional.
8. No regressions I can spot. The added lines are purely additive. The playerOnPlay body has two extra removeEventListener/addEventListener pairs that mirror the existing ended listener handling, same pattern, same place.
Not blocking. This is a high-quality fix with above-average test depth. LGTM with the optional closest() fallback test.
|
Added a dedicated test for the closest() fallback path — and went one step further than requested: a second case where elements.player is actually set, proving the fallback resets the speed rather than just failing safe. Ran a mutation sweep across the function's guard clauses afterward (each condition individually broken, then the suite rerun): 7/8 are caught, the miss being priority between closest() and elements.player in the ||, which doesn't affect real behavior since closest() always resolves the player in-browser. Thanks for the thorough review! |
…ed reset
closest('.html5-video-player') can return null; add a dedicated case for
that with no elements.player set (proves the early return) and one with
a valid elements.player (proves the fallback actually works, not just
fails safe).
6f16205 to
e10bb87
Compare
Summary
Resolves #4346
When a livestream is sped up through ImprovedTube (e.g. 5x) and catches up to the
live head, playback gets stuck in an endless stall → buffer → play loop.
Cause
ImprovedTube.playbackSpeed()setsvideo.playbackRatedirectly, so YouTube's playerstill reports 1x (
player.getPlaybackRate()returns 1 while the video runs at 5x).YouTube's own speed menu drops back to 1x as soon as it reaches the live head, but
since it doesn't know about our speed, it never does here.
Measured on a live stream (Chrome):
player.setPlaybackRate(2)(YouTube's API)video.playbackRate = 5(what ImprovedTube does)Changes
player.js: addplayerLiveHeadSpeedReset. Onwaiting, if the video is live,player.getProgressState().isAtLiveHeadis true and the rate is > 1, set the rateto 1 and show the usual speed status. This mirrors what YouTube's player already does.
functions.js: attach it as awaitinglistener next to the existing player listeners.tests/unit/live-head-speed-reset.test.js: covers the reset, DVR playback behind thelive head (speed kept), non-live videos, rates ≤ 1, and a missing player API.
Speeds above 2x stay available when watching a livestream behind the live head
(YouTube's own menu caps live at 2x); normal videos are not affected.
Testing
npm test: 124 passed (8 new). The new tests fail without the fix.and plays normally. After seeking 80s back and setting 5x, it keeps 5x while behind
and switches to 1x only when it catches up.